package a66weding.com.jiehuntong.common.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.JobDetail_Edit_Activity;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.date.NumericWheelAdapter;
import a66weding.com.jiehuntong.date.OnWheelScrollListener;
import a66weding.com.jiehuntong.date.WheelView;

/**
 * Created by admin on 2016/7/11.
 */
public class DataSelectDialogUtil {
    private static Context context;
    //对话框
    private Dialog dialog;
    private static DataSelectDialogUtil dataSelectDialogUtil;
    //取消键
    private TextView cancleTv;
    //确认键
    private TextView querenTv;
    private RelativeLayout rl;
    //布局解析器
    private LayoutInflater inflater;
    private WheelView year;
    private WheelView month;
    private WheelView day;
    private int mYear = 1996;
    private int mMonth = 0;
    private int mDay = 1;
    private View dateView;
    //需要传递的信息
    private String birthday;
    private JobDetail_Edit_Activity jobDetail_edit_activity;
    private DataSelectDialogUtil(Context context) {
        this.context = context;
        jobDetail_edit_activity = (JobDetail_Edit_Activity) context;
    }

    public static DataSelectDialogUtil getInstance(Context context) {
        if (dataSelectDialogUtil == null) {
            dataSelectDialogUtil = new DataSelectDialogUtil(context);
        }
        return dataSelectDialogUtil;
    }

    public void showDataDialgo() {
        inflater = (LayoutInflater) jobDetail_edit_activity.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        //添加日期选择器;
        rl = (RelativeLayout) dialog.findViewById(R.id.rl);
        rl.addView(getDataPick());
        dialog = new Dialog(jobDetail_edit_activity, R.style.dialog);
        //设置显示的视图
        dialog.setContentView(R.layout.diy_dialog_date);
        //点击变外不能取消
        dialog.setCanceledOnTouchOutside(false);
        cancleTv = (TextView) dialog.findViewById(R.id.diy_datecancel_tv);
        querenTv = (TextView) dialog.findViewById(R.id.diy_datequeren_tv);
        rl = (RelativeLayout) dialog.findViewById(R.id.rl);
        Window window = dialog.getWindow();
        // 可以在此设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 200;
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        dialog.onWindowAttributesChanged(wl);
        dialog.show();
        //设置title的点击事件
        cancleTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        querenTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送数据;
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setData(birthday);
                EventBus.getDefault().post( messageEvent);
            }
        });
    }

    /**
     * @return 婚期
     */
    private View getDataPick() {
        Calendar c = Calendar.getInstance();
        int norYear = c.get(Calendar.YEAR);

        int curYear = mYear;
        int curMonth = mMonth + 1;
        int curDate = mDay;

        dateView = inflater.inflate(R.layout.diy_dialog_date_picker, null);

        year = (WheelView) dateView.findViewById(R.id.year);
        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(jobDetail_edit_activity, 1900, norYear);
        numericWheelAdapter1.setLabel("年");
        year.setViewAdapter(numericWheelAdapter1);
        year.setCyclic(true);//是否可循环滑动
        year.addScrollingListener(scrollListener);

        month = (WheelView) dateView.findViewById(R.id.month);
        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(jobDetail_edit_activity, 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCyclic(true);
        month.addScrollingListener(scrollListener);

        day = (WheelView) dateView.findViewById(R.id.day);
        initDay(curYear, curMonth);
        day.setCyclic(true);

        year.setVisibleItems(7);//设置显示行数
        month.setVisibleItems(7);
        day.setVisibleItems(7);

        year.setCurrentItem(curYear - 1950);
        month.setCurrentItem(curMonth - 1);
        day.setCurrentItem(curDate - 1);
        return dateView;
    }
    //添加监听;
    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = year.getCurrentItem() + 1900;//年
            int n_month = month.getCurrentItem() + 1;//月

            initDay(n_year, n_month);

            birthday = new StringBuilder().append((year.getCurrentItem() + 1900)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem() + 1) < 10) ? "0" + (day.getCurrentItem() + 1) : (day.getCurrentItem() + 1)).toString();

        }
    };
    /**
     */
    private void initDay(int arg1, int arg2) {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(jobDetail_edit_activity, 1, getDay(arg1, arg2), "%02d");
        numericWheelAdapter.setLabel("日");
        day.setViewAdapter(numericWheelAdapter);

    }
    /**
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }
}
